/**
 * 访问量数据模型
 * 
 */
module.exports = class Pv extends require('./model'){
    /**
     *  获取总访问量
     */
    static getTotal(){
        return new Promise((resolve,reject) =>{
            let sql = 'SELECT SUM(hits) as hits FROM `pv`';
            // 父类继承的方法query
            this.query(sql).then(res =>{
                resolve(res[0].hits);
            }).catch(err =>{
                console.log(`数据库获取总访问量失败:${err.message}`);
                reject(err);
            });
        });
    }

    /**
     *  获取总类目数
     */
    static getCateNum(){
        return new Promise((resolve,reject) =>{
            let sql = 'SELECT count(id) as count FROM `category`';
            // 父类继承的方法query
            this.query(sql).then(res =>{
                resolve(res[0].count);
            }).catch(err =>{
                console.log(`数据库获取总访问量失败:${err.message}`);
                reject(err);
            });
        });
    }
    /**
     *  获取全部访问量信息
     *  时间段
     *  let sql = 'SELECT `time`,hits FROM `pv` WHERE `time`< ? AND `time` > ? ORDER BY `time` ASC';
     * 
     */
    static getAll(){
        return new Promise((resolve,reject) =>{
            let sql = 'SELECT `time`,hits FROM `pv` ORDER BY `time` ASC';
            // 父类继承的方法query
            this.query(sql).then(res =>{
                resolve(res);
            }).catch(err =>{
                console.log(`数据库获取总访问量失败:${err.message}`);
                reject(err);
            });
        });
    }
}